package cn.lg.soar.flow.api.model.node;

import cn.lg.soar.common.util.data.DataUtil;
import cn.lg.soar.flow.api.enums.ApproverMissingEnum;
import cn.lg.soar.flow.api.enums.NodeTypeEnum;
import cn.lg.soar.flow.api.enums.RejectTaskEnum;
import cn.lg.soar.flow.api.utils.XmlUtils;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Data
public class ApprovalNode extends UserNode implements Node, Serializable {

    private NodeTypeEnum instanceType;

    private String completionCondition;

    private Float ratio;

    private List<Assignee> assignees;

    private ApproverMissingEnum missingAssignee;

    private Long defaultAssignee;

    private RejectTaskEnum backType;

    private String backNode;



    @JsonProperty("type")
    public String getType() { return "approval"; }

    @Override
    public String toXmlString() {
        StringBuilder sb = new StringBuilder();
        // 开始构建 userTask 标签
        sb.append("<userTask id=\"").append(key)
                .append("\" name=\"").append(XmlUtils.escapeXml(name)).append("\" ");
        if (NodeTypeEnum.OR_SIGN.equals(instanceType)) {
            sb.append("flowable:candidateUsers=\"${taskApproverHandler.approverStr(execution)}\" ");
        } else {
            sb.append("flowable:candidateUsers=\"${approver}\" ");
        }
        sb.append(">\n");
        sb.append("<extensionElements>");
        sb.append("<flowable:taskListener event=\"create\" delegateExpression=\"${innerTaskListener}\" />");
        // 添加流程监听器
        if (DataUtil.isValuable(processListeners)) {
            for (ProcessListener x : processListeners) {
                sb.append("<flowable:executionListener event=\"").append(x.getEvent()).append("\" ")
                        .append(Boolean.TRUE.equals(x.getIsExpression()) ? "expression" : "class")
                        .append("=\"").append(XmlUtils.escapeXml(x.getValue())).append("\" />\n");
            }
        }
        // 添加任务监听器
        if (DataUtil.isValuable(nodeListeners)) {
            for (NodeListener x : nodeListeners) {
                sb.append("<flowable:taskListener event=\"").append(x.getEvent().name()).append("\" ")
                        .append(Boolean.TRUE.equals(x.getIsExpression()) ? "expression" : "class")
                        .append("=\"").append(XmlUtils.escapeXml(x.getValue())).append("\" />\n");
            }
        }
        sb.append("</extensionElements>\n");
        // 多实例
        switch (instanceType) {
            case SERIAL -> {
                // 串行
                sb.append("<multiInstanceLoopCharacteristics isSequential=\"true\" " +
                                "flowable:collection=\"${taskApproverHandler.approverList(execution)}\" ")
                        .append("flowable:elementVariable=\"approver\">")
                        .append("</multiInstanceLoopCharacteristics>\n");
            }
            case JOIN_SIGN -> {
                // 会签
                sb.append("<multiInstanceLoopCharacteristics isSequential=\"false\" " +
                        "flowable:collection=\"${taskApproverHandler.approverList(execution)}\" ")
                        .append("flowable:elementVariable=\"approver\">\n");
                if (DataUtil.isValuable(completionCondition)) {
                    sb.append("<completionCondition>").append(completionCondition).append("</completionCondition>\n");
                }
                sb.append("</multiInstanceLoopCharacteristics>\n");
            }
            default -> {}
        }
        sb.append("</userTask>\n");
        return sb.toString();
    }

}